package com.ruoyi.procurement.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.BaseUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.procurement.service.IPsGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.procurement.mapper.PsProcureMapper;
import com.ruoyi.procurement.domain.PsProcure;
import com.ruoyi.procurement.service.IPsProcureService;

/**
 * 采购记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-10
 */
@Service
public class PsProcureServiceImpl implements IPsProcureService 
{
    @Autowired
    private PsProcureMapper psProcureMapper;
    @Autowired
    private IPsGoodsService iPsGoodsService;

    /**
     * 查询采购记录
     * 
     * @param id 采购记录主键
     * @return 采购记录
     */
    @Override
    public PsProcure selectPsProcureById(String id)
    {
        return psProcureMapper.selectPsProcureById(id);
    }

    /**
     * 查询采购记录列表
     * 
     * @param psProcure 采购记录
     * @return 采购记录
     */
    @Override
    public List<PsProcure> selectPsProcureList(PsProcure psProcure)
    {
        Map<String, Object> params = psProcure.getParams();
        Object pstType = params.get("pstType");
        if (pstType!=null){
            String s = pstType.toString();
            if (StringUtils.isNotEmpty(s)){
                ArrayList<String> listParmas = new ArrayList<>(Arrays.asList(s.split(",")));
                params.put("pstType",listParmas);
            }
        }
        return psProcureMapper.selectPsProcureList(psProcure);
    }

    /**
     * 新增采购记录
     * 
     * @param psProcure 采购记录
     * @return 结果
     */
    @Override
    public int insertPsProcure(PsProcure psProcure)
    {
        psProcure.setCreateTime(DateUtils.getNowDate());
        return psProcureMapper.insert(psProcure);
    }

    /**
     * 修改采购记录
     * 
     * @param psProcure 采购记录
     * @return 结果
     */
    @Override
    public int updatePsProcure(PsProcure psProcure)
    {
        psProcure.setUpdateTime(DateUtils.getNowDate());
        return psProcureMapper.updateById(psProcure);
    }

    /**
     * 批量删除采购记录
     * 
     * @param ids 需要删除的采购记录主键
     * @return 结果
     */
    @Override
    public int deletePsProcureByIds(String[] ids)
    {
        for (String id : ids) {
            PsProcure psProcure = selectPsProcureById(id);
            iPsGoodsService.deduction(null,"采购回退",null,psProcure,null);
        }
        return psProcureMapper.deletePsProcureByIds(ids);
    }

    /**
     * 删除采购记录信息
     * 
     * @param id 采购记录主键
     * @return 结果
     */
    @Override
    public int deletePsProcureById(String id)
    {
        return psProcureMapper.deletePsProcureById(id);
    }

    @Override
    public void deleteByPsGoodsId(String goodsId) {
        psProcureMapper.deleteByPsGoodsId(goodsId);
    }
}
